package com.hsq.androidproject.ui.fragment;


import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.lenovo.androidproject3.R;
import com.hsq.androidproject.adapter.MyGridViewFindAdapter;
import com.hsq.androidproject.adapter.MyListViewFindHotAdapter;
import com.hsq.androidproject.adapter.MyListViewFindMineAdapter;
import com.hsq.androidproject.entity.find.FindItems;
import com.hsq.androidproject.entity.find.FindMinesAndHots;
import com.hsq.androidproject.ui.SearchActivity;
import com.hsq.androidproject.utils.OkHttpClientManager;
import com.hsq.androidproject.utils.glide.GlideCircleTransform;
import com.hsq.androidproject.weight.pulldown.PullDownElasticImp;
import com.hsq.androidproject.weight.pulldown.PullDownScrollView;
import com.google.gson.Gson;
import com.squareup.okhttp.Request;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class FindFragment extends BaseFragment implements PullDownScrollView.RefreshListener {

    private TextView tv_text;
    private GridView mGridViewFind;
    private MyGridViewFindAdapter adapter;
    private LinearLayout ll_more;
    private ImageView iv_img;

    /*我的讨论区*/
    private ListView mMineListView;

    /*热门讨论区*/
    private ListView mHotListView;

    private PullDownScrollView mPullDownScrollView;
    private MyListViewFindMineAdapter findMineadapter;
    private MyListViewFindHotAdapter findHotadapter;

    private ScrollView sv_container;

    public FindFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_find, container, false);
        initView(view);
        mPullDownScrollView = (PullDownScrollView) view.findViewById(R.id.refresh_root);
        mPullDownScrollView.setRefreshListener(this);
        mPullDownScrollView.setPullDownElastic(new PullDownElasticImp(getContext()));
        sv_container = (ScrollView) view.findViewById(R.id.sv_container);
        sv_container.smoothScrollTo(0, 0);

        /*最上边的8个小布局*/
        OkHttpClientManager.getAsyn("http://api.hanju.koudaibaobao.com/api/series/discoveryV4", new OkHttpClientManager.ResultCallback<String>() {
            @Override
            public void onError(Request request, Exception e) {
                Toast.makeText(getContext(), "error：" + e.getMessage(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(String response) {
                FindItems findItems = new Gson().fromJson(response, FindItems.class);
                List<FindItems.ItemsBean> itemsBean = findItems.getItems();
                adapter = new MyGridViewFindAdapter(itemsBean, getContext());
                mGridViewFind.setAdapter(adapter);
            }
        });

        Glide.with(getContext())
                .load(R.drawable.category_draft)
                .transform(new GlideCircleTransform(getContext()))//圆形
                .into(iv_img);

        ll_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(activity, "投稿暂未开放", Toast.LENGTH_SHORT).show();
            }
        });

        //http://api.hanju.koudaibaobao.com/bbs/api/forum/getBoardsV2
        OkHttpClientManager.getAsyn("http://api.hanju.koudaibaobao.com/bbs/api/forum/getBoardsV2", new OkHttpClientManager.ResultCallback<String>() {
            @Override
            public void onError(Request request, Exception e) {
                Toast.makeText(getContext(), "error：" + e.getMessage(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(String response) {
                FindMinesAndHots minesAndHot = new Gson().fromJson(response, FindMinesAndHots.class);
                /*我的讨论区*/
                List<FindMinesAndHots.MinesBean> itemsBean = minesAndHot.getMines();
                findMineadapter = new MyListViewFindMineAdapter(itemsBean, getContext());
                mMineListView.setAdapter(findMineadapter);
                /*热门讨论区*/
                List<FindMinesAndHots.HotsBean> hotsBean = minesAndHot.getHots();
                findHotadapter = new MyListViewFindHotAdapter(hotsBean, getContext());
                mHotListView.setAdapter(findHotadapter);
            }
        });

        return view;
    }

    @Override
    public void onRefresh(PullDownScrollView view) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                // mPullDownScrollView.finishRefresh("上次刷新时间:12:23");
                mPullDownScrollView.finishRefresh("正在刷新");
                mGridViewFind.setFocusable(false);
                OkHttpClientManager.getAsyn("http://api.hanju.koudaibaobao.com/bbs/api/forum/getBoardsV2", new OkHttpClientManager.ResultCallback<String>() {
                    @Override
                    public void onError(Request request, Exception e) {
                        Toast.makeText(getContext(), "error：" + e.getMessage(), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response) {
                        FindMinesAndHots minesAndHot = new Gson().fromJson(response, FindMinesAndHots.class);
                     /*我的讨论区*/
                        List<FindMinesAndHots.MinesBean> itemsBean = minesAndHot.getMines();
                        findMineadapter = new MyListViewFindMineAdapter(itemsBean, getContext());
                        mMineListView.setAdapter(findMineadapter);
                      /*热门讨论区*/
                        List<FindMinesAndHots.HotsBean> hotsBean = minesAndHot.getHots();
                        findHotadapter = new MyListViewFindHotAdapter(hotsBean, getContext());
                        mHotListView.setAdapter(findHotadapter);
                    }
                });
                mGridViewFind.setFocusable(false);
            }
        }, 2000);
    }


    private void initView(View view) {
        tv_text = (TextView) view.findViewById(R.id.tv_text);
        mGridViewFind = (GridView) view.findViewById(R.id.mGridViewFind);
        ll_more = (LinearLayout) view.findViewById(R.id.ll_more);
        mMineListView = (ListView) view.findViewById(R.id.mMineListView);
        mHotListView = (ListView) view.findViewById(R.id.mHotListView);
        iv_img = (ImageView) view.findViewById(R.id.iv_img);
        tv_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent2Activity(SearchActivity.class);
            }
        });
    }


}
